package com.yxp.offer;

/**
 * @author : yang.xp
 * @date : 2021/8/25 9:19
 * @description : 剑指 Offer 21. 调整数组顺序使奇数位于偶数前面
 */
public class Problem21 {

    public static void main(String[] args) {
        Problem21 problem21 = new Problem21();
        int[] numbers = new int[]{1, 2, 3, 4, 7, 10, 11, 3};
        int[] exchange = problem21.exchange(numbers);
        for (int i = 0; i < exchange.length; i++) {
            System.out.println(exchange[i]);
        }
    }

    public int[] exchange(int[] numbers) {
        if (numbers == null || numbers.length == 0) {
            return new int[]{};
        }
        int firstO = 0;
        for (int i = 0; i < numbers.length; i++) {
            if (numbers[i] % 2 == 1) {
                int temp = numbers[firstO];
                numbers[firstO] = numbers[i];
                numbers[i] = temp;
                // 每次只要交换，肯定要指向下一位。
                firstO++;
            }
        }
        return numbers;
    }
}
